package cvs;
import java.text.DecimalFormat;
import java.text.NumberFormat;

public class TestMain {

	public static void main(String[] args) {

		//testConvertNumber();

		digit("");
		digit("1");
		digit("12");
		digit("12345");
		digit("123456");
		digit("1234567");
		digit("12345678");
		digit("123456789");
		digit("1234567890");
	}

	private static void digit(String duns_number)
	{
		int digit_duns=9;
	
		duns_number = duns_number.trim();

		if (duns_number.length() == 0 || duns_number.length() >= digit_duns)
			;
		else {
			// make 9 digit
			int diff = digit_duns - duns_number.length();

			for (int i = 0; i < diff; i++) {
				duns_number = '0' + duns_number;
			}
		}
		
		System.out.println("duns=" + duns_number + " . " + duns_number.length());
	}
	
	private static void testConvertNumber() {
		try {
			System.out
					.println(">>" + TestMain.testConvertNumber("1234") + "<<");
			System.out.println(">>" + TestMain.testConvertNumber("") + "<<");
			System.out.println(">>"
					+ TestMain.testConvertNumber("- 12,234.024") + "<<");
			System.out.println(">>" + TestMain.testConvertNumber(" 12,234.024")
					+ "<<");
			System.out.println(">>"
					+ TestMain.testConvertNumber(" $341212,234.026") + "<<");
			System.out.println(">>"
					+ TestMain.testConvertNumber(" $12,23,4.024") + "<<");
			System.out.println(">>" + TestMain.testConvertNumber("(12234.024)")
					+ "<<");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static String convertTwoDigits(String str) {
		if (str == null || str.length() == 0)
			return "0";

		try {
			int num = Integer.parseInt(str);

			if (num > 0 && num < 10)
				return "0" + num;

		} catch (Exception e) {
		}
		return str;
	}

	public static String testConvertNumber(String str) {
		try {
			return convertNumber(str);
		} catch (Exception e) {
			return e.getMessage();
		}
	}

	public static String convertNumber(String str) throws Exception {
		if (str == null)
			return "0";

		str = str.trim();

		if (str.startsWith("(") && str.endsWith(")"))
			str = "-" + str;

		StringBuffer sb = new StringBuffer();

		char[] ch = str.toCharArray();
		for (char c : ch) {
			if (c != ' ' && c != ',' && c != '$' && c != '(' && c != ')')
				sb.append(c);
		}

		String result = sb.toString();

		try {
			Double value = Double.parseDouble(result);
			
			NumberFormat format = new DecimalFormat("#.##");
			return format.format(value);
		} catch (Exception e) {
			throw e;
		}
	}
}
